var app = angular.module('mymanage', ['ngCookies']);

app.controller('mainController', mainController);
app.controller('loginController', loginController);
app.controller('headController', headController);
app.controller('persondataController', persondataController);
app.controller('homepageController', homepageController);
app.controller('alterController', alterController);
app.controller('addstudentController', addstudentController);
app.controller('addclassController', addclassController);
app.controller('classmsgController', classmsgController);
app.controller('stuclassmsgController', stuclassmsgController);

function mainController($scope) {}
// 登录控制器
function loginController($scope, $http) {
    $scope.flag = false;
    $scope.identity = '';
    $(".question").on("mouseover", function () {
        $scope.flag = true
        $scope.$apply();
    }).on("mouseout", function () {
        $scope.flag = false
        $scope.$apply();
    })

    $scope.login = function () {

        $http.post("/login", {
            account: $scope.account,
            password: $scope.password,
            identity: $scope.identity,
        }).then(function (res) {
            if (res.data.code != 200) {
                layer.msg(res.data.message);
                return
            }
            layer.msg("登录成功，自动跳转中.....");
            window.setTimeout("window.location='/users/homepage'", 1000);
        }, function (err) {
            console.log(err)
        })
    }
}
// 主页控制器
function homepageController($scope, $http) {
 
}
//头部控制器
function headController($scope,$rootScope, $http) {
    var $lis = $(".headcontain  ul li")
    var $divs = $(".headcontain  ul li .tabdiv")
    $lis.each(function (index) {
        $(this).on("mouseenter", function () {
            $(this).addClass('current_li');
            $divs.eq(index).addClass('current_div');
        }).on("mouseleave", function (e) {
            $(this).removeClass("current_li");
            $divs.eq(index).removeClass('current_div');
        })
    });

    var $daynight = $(".daynight span");
    $daynight.eq(0).addClass("daynight1");
    var body = document.querySelector("body")
    var head = document.querySelector(".head")
    $daynight.each(function (index) {
        $(this).on("click", function () {
            $daynight.eq(0).removeClass();
            $daynight.eq(1).removeClass();
            (index == 0) ? (body.style.backgroundColor = "#eee", head.style.backgroundColor = "#eee") : (body.style.backgroundColor = "#000033", head.style.backgroundColor = "#000033");
            (index == 1) ? (body.style.backgroundColor = "#000033", head.style.backgroundColor = "#000033") : (body.style.backgroundColor = "#eee", head.style.backgroundColor = "#2EAFBB");
            $(this).addClass('daynight1');
        })
    });

    $scope.logout = function () {
        layer.open({
            content: '您确认退出？',
            btn: ['确认', '取消'],
            shadeClose: false,
            yes: function () {
                location.href = "/users/logout"
            }, no: function () {
            }
        });
    };

}

//个人资料页面控制器
function persondataController($scope, $http) {
    $scope.alterdata = function () {

        var data = {
            //21
            studentNo: $scope.studentNo,
            name: $scope.name,
            age: $scope.age,
            sex: $scope.sex,
            phone: $scope.phone,
            ID: $scope.ID,
            weixin: $scope.weixin,
            qq: $scope.qq,
            email: $scope.email,
            school: $scope.school,
            major: $scope.major,
            education: $scope.education,
            joinDate: $scope.joinDate,
            gradDate: $scope.gradDate,
            studyStatus: $scope.studyStatus,
            introTeacher: $scope.introTeacher,
            chargeTeacher: $scope.chargeTeacher,
            teachTeacher: $scope.teachTeacher,
            belongClass: $scope.belongClass,
            dormAddress: $scope.dormAddress,
            dormNo: $scope.dormNo,
        };

        $http.post("/users/alterdata", {
            data: data,
        }).then(function (res) {

            if (res.data.code != 200) {
                layer.msg(res.data.message);
                return;
            }
            layer.msg("修改信息成功");
            window.setTimeout(`window.location='/users/persondata?studentNo='+${$scope.studentNo}`, 1000);
        }, function (err) {
            console.log(err)
        })
    };
    $scope.delete = function(){
        layer.open({
            content: '删除后将不能找回数据，确认删除信息吗？',
            btn: ['确认', '取消'],
            shadeClose: false,
            yes: function () {
                $http.post("/teachers/deletestudent",{studentNo: $scope.studentNo}).then(function(res){
                    if(res.data.code !=200 ){
                        layer.msg(res.data.message);
                        return;
                    }
                    layer.msg("删除信息成功");
                    window.setTimeout(`window.location='/users/homepage'`, 1000);
                },function(err){
                    console.log(err)
                })
            }, no: function () {
            }
        });
       
    }
}
//修改密码控制器
function alterController($scope, $http) {
    $scope.abandon = function () {
        layer.open({
            content: '您确认舍弃更改？',
            btn: ['确认', '取消'],
            shadeClose: false,
            yes: function () {
                location.href = "/users/homepage"
            }, no: function () {
            }
        });
    };

    $scope.alter = function () {
        if ($scope.sessionaccount.toString() != $scope.account.toString()) {
            layer.msg("您只能修改自己的密码，请重新输入账号");
            return
        }
        if ($scope.password.toString() != $scope.confirmpwd.toString()) {
            layer.msg("您两次输入的密码不一致，请重新输入");
            return
        }
        layer.open({
            content: '本系统不提供未登录状态找回密码，请您牢记修改的密码，若忘记密码请及时联系管理员，确认修改密码？',
            btn: ['确认', '取消'],
            shadeClose: false,
            yes: function () {
                $http.post("/users/alterpwd", {
                    account: $scope.account,
                    password: $scope.password,
                }).then(function (res) {
                    if (res.data.code != 200) {
                        layer.msg(res.data.message);
                        return;
                    }
                    layer.msg("修改密码成功，请重新登录.....");
                    window.setTimeout("window.location='/'", 1000);
                }, function (err) {
                    console.log(err)
                })
            }, no: function () {
            }
        });
    }
}
//添加学生控制器
function addstudentController($scope, $http) {
    $scope.abandon = function () {
        layer.open({
            content: '您确认舍弃添加的信息？',
            btn: ['确认', '取消'],
            shadeClose: false,
            yes: function () {
                location.href = "/users/homepage"
            }, no: function () {
            }
        });
    };

    $scope.addmsg = function () {
        $.post("/users/getclassMsg", { className: $scope.className }, function (res) {

            var classNameId = res.message[0][0]._id
            var data = {
                studentNo: $scope.studentNo,
                name: $scope.name,
                age: $scope.age,
                sex: $scope.sex,
                phone: $scope.phone,
                ID: $scope.ID,
                weixin: $scope.weixin,
                QQ: $scope.QQ,
                email: $scope.email,
                school: $scope.school,
                major: $scope.major,
                education: $scope.education,
                joinDate: $scope.joinDate,
                gradDate: $scope.gradDate,
                studyStatus: $scope.studyStatus,
                introTeacher: $scope.introTeacher,
                chargeTeacher: $scope.chargeTeacher,
                teachTeacher: $scope.teachTeacher,
                className: $scope.className,
                classNameId: classNameId,
                dormAddress: $scope.dormAddress,
                dormNo: $scope.dormNo,
                pay: $scope.pay,

            };
            var studentNo = $scope.studentNo;
            $http.post("/teachers/addstudent", {
                data: data,
            }).then(function (res) {
                if (res.data.code != 200) {
                    layer.msg(res.data.message);
                    return;
                }
                layer.msg("添加学生信息成功");
                window.setTimeout(`window.location='/users/persondata?studentNo='+${studentNo}`, 1000);
            }, function (err) {
                console.log(err)
            })
        })
    }
}
//添加班级控制器
function addclassController($scope, $http) {
    $scope.abandon = function () {
        layer.open({
            content: '您确认舍弃添加的信息？',
            btn: ['确认', '取消'],
            shadeClose: false,
            yes: function () {
                location.href = "/users/homepage"
            }, no: function () {
            }
        });
    };
    $scope.addmsg = function () {

        var data = {
            teacherId: $scope.teacherId,
            className: $scope.className,
            personCount: $scope.personCount,
            chargeTeacher: $scope.chargeTeacher,
            teachTeacher: $scope.teachTeacher,
            jobTeacher: $scope.jobTeacher,
            assistant: $scope.assistant,
            classStatus: $scope.classStatus,
            classAddress: $scope.classAddress,
            courseName: $scope.courseName,
            startDate: $scope.startDate,
            endDate: $scope.endDate,
            officialDate: $scope.officialDate,
        };
        $http.post("/teachers/addclass", {
            data: data
        }).then(function (res) {
            if (res.data.code != 200) {
                layer.msg(res.data.message);
                return
            }
            layer.msg("添加班级成功");
            window.setTimeout("window.location='/users/homepage'", 1000);
        }, function (err) {
            console.log(err)
        })
    }
}


function classmsgController($scope, $http) {
    $scope.selectchange = function () {
        $.post("/users/getclassMsg", { className: $scope.current.className }, function (res) {
            $scope.className=res.message[0][0].className;
            $scope.personCount=res.message[1];
            $scope.chargeTeacher=res.message[0][0].chargeTeacher;
            $scope.teachTeacher=res.message[0][0].teachTeacher;
            $scope.jobTeacher=res.message[0][0].jobTeacher;
            $scope.assistant=res.message[0][0].assistant;
            $scope.classStatus=res.message[0][0].classStatus;
            $scope.classAddress=res.message[0][0].classAddress;
            $scope.courseName=res.message[0][0].courseName;
            $scope.startDate=res.message[0][0].startDate;
            $scope.officialDate=res.message[0][0].officialDate;
            $scope.endDate=res.message[0][0].endDate;
        
            $scope.$apply();
        })
        $.post("/users/getstudentMsg",{className: $scope.current.className},function(res){
            $scope.students = res.message[0];
            $scope.$apply();
        })
    }

}
function stuclassmsgController($scope,$http){
   
}